<?php

namespace plugin\cms\app\controller\uc;

use app\bll\Site as BllSite;
use core\controller\UcBase;
use Exception;
use plugin\cms\app\bll\Collect as BllCollect;
use plugin\cms\app\bll\Model as BllModel;
use plugin\cms\app\bll\Praise as BllPraise;
use support\View;
use plugin\cms\app\bll\Category as BllCategory;

/**
 * 内容点赞和收藏
 */
class PraiseCollect extends UcBase
{

    public function index()
    {
        $optype = request()->get('optype', '');
        if (in_array($optype, ['selCollect', 'selPraise'])) {
            switch ($optype) {
                case 'selCollect':
                    $bllCollect = new BllCollect();
                    $records = $bllCollect->paginate(
                        mid_mtablename: request()->post('tablename'),
                        uid: request()->userinfo['id'],
                        pagesize: 10,

                    );
                    break;
                case 'selPraise':
                    $bllPraise = new BllPraise();
                    $records = $bllPraise->paginate(
                        mid_mtablename: request()->post('tablename'),
                        uid: request()->userinfo['id'],
                        pagesize: 10,

                    );

                    break;
            }

            $bllCategory = new BllCategory();
            $bllSite     = new BllSite();
            $sites       = $bllSite->select();
            $sites       = array_column($sites, null, 'id');

            foreach ($records as $key => $record) {
                $ancestorsCates = $bllCategory->getAncestorsAndSelf($record->cid, $record->siteid);
                $ancestorsCatetitle = '';
                foreach ($ancestorsCates as $acate) {
                    $ancestorsCatetitle .= $acate['title'] . ' ';
                }
                $record->ancestorsCatetitle =  $ancestorsCatetitle;
                $record->contenturl = $sites[$record->siteid]['protocol'] . $sites[$record->siteid]['domain'] . cms_content_url($record);
            }

            return $this->success([
                'items' => $records->items(),
                'total' => $records->total(),
            ]);
        }

        //获取模型
        $bllModel = new BllModel();
        $models = $bllModel->select();
        View::assign('models', $models);
        return $this->view();
    }

    /**
     * 收藏
     *
     * @return mixed
     */
    public function doCollect()
    {
        $result = 0;
        try {
            $bllCollect = new BllCollect();
            $iscollect = $bllCollect->isCollect(
                uid: request()->userinfo['id'],
                contentid: request()->post('contentid'),
                cid: request()->post('cid'),
                siteid: request()->post('siteid'),
            );

            if ($iscollect) {
                $bllCollect->cancelCollect(
                    uid: request()->userinfo['id'],
                    contentid: request()->post('contentid'),
                    cid: request()->post('cid'),
                    siteid: request()->post('siteid'),
                );
                $result = -1;
            } else {
                $bllCollect->doCollect(
                    uid: request()->userinfo['id'],
                    contentid: request()->post('contentid'),
                    cid: request()->post('cid'),
                    siteid: request()->post('siteid'),
                );
                $result = 1;
            }
        } catch (Exception $ex) {
            return $this->error($ex->getMessage());
        }
        return $this->success(['result' => $result]);
    }

    /**
     * 点赞
     *
     * @return mixed
     */
    public function doPraise()
    {
        $result = 0;
        try {
            $bllPraise = new BllPraise();
            $ispraise = $bllPraise->isPraise(
                uid: request()->userinfo['id'],
                contentid: request()->post('contentid'),
                cid: request()->post('cid'),
                siteid: request()->post('siteid'),
            );

            if ($ispraise) {
                $bllPraise->cancelPraise(
                    uid: request()->userinfo['id'],
                    contentid: request()->post('contentid'),
                    cid: request()->post('cid'),
                    siteid: request()->post('siteid'),
                );
                $result = -1;
            } else {
                $bllPraise->doPraise(
                    uid: request()->userinfo['id'],
                    contentid: request()->post('contentid'),
                    cid: request()->post('cid'),
                    siteid: request()->post('siteid'),
                );
                $result = 1;
            }
        } catch (Exception $ex) {
            return $this->error($ex->getMessage());
        }
        return $this->success(['result' => $result]);
    }
}
